package com.heima.wemedia.builder;

import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import org.springframework.beans.BeanUtils;

import java.util.Date;

/**
 * 构建者模式
 */
public class ArticleDtoBuilder {


    private WmNews wmNews;
    private WmChannelMapper wmChannelMapper;
    private WmUserMapper wmUserMapper;

    private ArticleDto dto = new ArticleDto();

    public ArticleDtoBuilder(WmNews wmNews, WmChannelMapper wmChannelMapper, WmUserMapper wmUserMapper){
        this.wmNews = wmNews;
        this.wmChannelMapper = wmChannelMapper;
        this.wmUserMapper = wmUserMapper;
    }

    public ArticleDtoBuilder buildBeanCopy(){
        BeanUtils.copyProperties(wmNews ,dto);
        return this;
    }

    public ArticleDtoBuilder buildBasic(){
        dto.setLayout(wmNews.getType());
        dto.setCreatedTime(new Date());
        dto.setCollection(0);
        dto.setComment(0);
        dto.setViews(0);
        dto.setLikes(0);
        return this;
    }

    public ArticleDtoBuilder buildAuth(){
        WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
        dto.setAuthorId((long)wmNews.getUserId());
        if (wmNews != null) {
            dto.setAuthorName(wmUser.getName());
        }
        return this;
    }
    public ArticleDtoBuilder buildChannel(){
        WmChannel wmChannel = wmChannelMapper.selectById(wmNews.getChannelId());
        if (wmChannel != null) {
            dto.setChannelName(wmChannel.getName());
        }
        return this;
    }

    public ArticleDto builder(){
        return dto;
    }
}
